package com.haofengsoft.scce.ui.consultation;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.GuestColumnAdapter;
import com.haofengsoft.scce.adapter.information.GuestColumnModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.controller.consultation.GuestColumnController;
import com.haofengsoft.scce.ui.fragment.BaseFragment;

public class GuestColumnFragment extends BaseFragment {

    private PullToRefreshListView guestColumnPtrl;
    private GuestColumnAdapter guestColumnAdapter = null;

    private int pageIndex = 0;
    private Context context;


    public static GuestColumnFragment newInstance(String name) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        GuestColumnFragment fragment = new GuestColumnFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.consultation_guest_column, container, false);
        view.setOnTouchListener(new DontSpillOnTouchListener());
        context = getActivity();
        view.setOnTouchListener(new DontSpillOnTouchListener());
        guestColumnPtrl = (PullToRefreshListView) view.findViewById(R.id.consultation_guest_column_ptrl);
        guestColumnPtrl.setMode(PullToRefreshBase.Mode.DISABLED);

        ImageView nodataIv = (ImageView) view.findViewById(R.id.guest_column_no_data_iv);
        nodataIv.setOnClickListener(new DataLoadOnClickListener());
        guestColumnPtrl.setEmptyView(nodataIv);
        guestColumnPtrl.setOnFocusChangeListener(null);
        guestColumnPtrl.setOnHierarchyChangeListener(null);
        guestColumnPtrl.setOnRefreshListener(new LoadPullData());
        initVolleyData();
        return view;
    }

    @Override
    protected void lazyLoad() {

    }

    class DataLoadOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initVolleyData();
        }
    }

    class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
        // 下拉(手势下拉)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            pageIndex = 0;
            guestColumnAdapter = null;
            initVolleyData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            initVolleyData();
        }
    }

    private enum GuestColumn {
        ONE("牛股汇：中期业绩成试金石三金发力周涨6...", "（腾讯证券讯）本周A股灾后重建第二周，沪指一度站上4100点，最低下探至3...", "07-25 07:05"),

        TWO("涨停板预测：天广消防等17股有望冲涨停", "腾讯证券每日晚间根据上市公司公告、财务报表以及市场热点等多方面内容，甄选出...", "07-24 21:06"),

        THREE("揭秘涨停板：电改顶层设计呼之欲出 3股涨停", "（腾讯证券华烨发自北京）周五两市双双高开，早盘石油、煤炭、有色接力拉升股指...", "07-24 15：31"),

        FOUR("涨停板预测：三湘股份等24股有望冲涨停", "本文根据上市公司公告、财务报表以及市场热点等多方面内容，甄选出部分有望冲刺...", "07-23 21：11"),

        FIVE("揭秘涨停板：文科园林等129只次新股掀狂潮", "（腾讯证券华烨发自北京）周五沪指涨逾2%突破4100点，实现六连阳，板块全...", "07-23 16：09");

        String title;
        String overView;
        String time;

        GuestColumn(String title, String overView, String time) {
            this.title = title;
            this.overView = overView;
            this.time = time;
        }

        public String getTitle() {
            return title;
        }

        public String getOverView() {
            return overView;
        }

        public String getTime() {
            return time;
        }

    }

    private void refreshList(List<GuestColumnModel> modelList) {
        if (null != modelList) {
            if (null == guestColumnAdapter) {
                guestColumnAdapter = new GuestColumnAdapter(context, modelList);
                guestColumnPtrl.setAdapter(guestColumnAdapter);
            } else {
                guestColumnAdapter.addGuestColumnList(modelList);
            }
            pageIndex = pageIndex + 1;
            guestColumnAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), "没有信息了", Toast.LENGTH_SHORT).show();
        }
        guestColumnPtrl.onRefreshComplete();
    }

    private void initVolleyData() {

        List<GuestColumnModel> modelList = new ArrayList<GuestColumnModel>();
        for (GuestColumn guestColumn : GuestColumn.values()) {
            GuestColumnModel guestColumnModel = new GuestColumnModel();
            guestColumnModel.setId("3");
            guestColumnModel.setDetail(guestColumn.getOverView());
            guestColumnModel.setSoure("腾讯证券");
            guestColumnModel.setTime(guestColumn.getTime());
            guestColumnModel.setTitle(guestColumn.getTitle());
            modelList.add(guestColumnModel);
        }

        refreshList(modelList);

        // Map<String, Object> mapParam = new HashMap<String, Object>();
        // mapParam.put("PageIndex", pageIndex);
        // mapParam.put("PageSize", 10);
        // mapParam.put("TypeID", 1);
        //
        // VolleyManage.getInstance().methodPost(getActivity(), null,
        // ConstantConfig.TYPE_NEWS_URL, mapParam,
        // new GuestColumnVolleyReal());

    }

    class GuestColumnVolleyReal implements VolleyInterface {
        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                List<GuestColumnModel> modelList = GuestColumnController.getInstance(getActivity())
                        .handleGuestColumnModelList(bean.getContent());
                refreshList(modelList);
            } else {
                Toast.makeText(getActivity(), "网络连接错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
